#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <queue>

using namespace std;
using LL = long long;
using PII = pair<int, int>;

int n;
priority_queue<int, vector<int>, greater<int>> q;

int main(){
#ifdef LOCALFLAG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif


    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;

    LL res = 0;
    for(int i = 1, x; i <= n; i ++){
        cin >> x;
        q.push(x);
    }

    while(q.size() > 1){
        LL x1 = q.top();
        q.pop();
        LL x2 = q.top();
        q.pop();
        res += x1 + x2;
        q.push(x1 + x2);
    }

    cout << res << '\n';




    return 0;
}